package com.zadtek.jpsqishi.activity;

import android.app.AlertDialog.Builder;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.zadtek.jpsqishi.R;
import com.zadtek.jpsqishi.activity.common.Common;
import com.zadtek.jpsqishi.activity.common.Const;
import com.zadtek.jpsqishi.frame.MyApplication;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;

/**
 * 设置
 * 
 * @author Administrator
 * 
 */
public class SettingActivity extends BaseActivity implements OnClickListener {
	private ImageView backid;
	private TextView topview;
	private TextView set_location;
	private RelativeLayout messages, newhao, mylocation, finishs;
	private SharedPreferences sp; // 用户信息缓存
	private Editor editor;

	String version;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);

		MyApplication.getInstace().addActivity(this);
		PackageManager manager = this.getPackageManager();
		PackageInfo info;

		backid = (ImageView) findViewById(R.id.backid);
		topview = (TextView) findViewById(R.id.topview);
		topview.setText("设置");

		messages = (RelativeLayout) findViewById(R.id.messages);
		newhao = (RelativeLayout) findViewById(R.id.newhao);
		mylocation = (RelativeLayout) findViewById(R.id.mylocation);
		finishs = (RelativeLayout) findViewById(R.id.finishs);
		set_location = (TextView) findViewById(R.id.set_location);

		backid.setOnClickListener(this);
		messages.setOnClickListener(this);
		newhao.setOnClickListener(this);
		mylocation.setOnClickListener(this);
		finishs.setOnClickListener(this);

		try {
			info = manager.getPackageInfo(this.getPackageName(), 0);
			version = info.versionName;
			set_location.setText("V" + version);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch (msg.what) {
				case Const.MSG_SET_ALIAS:// 绑定别名
					JPushInterface.setAliasAndTags(getApplicationContext(),
							(String) msg.obj, null, mAliasCallback);
					break;
				}
			}
		};
	}

	@Override
	public void onClick(View arg0) {
		Intent intent;
		switch (arg0.getId()) {
		case R.id.backid:
			this.finish();
			break;

		case R.id.messages:// 管理细则
			intent = new Intent(SettingActivity.this, ManagementRules.class);
			startActivity(intent);
			break;

		case R.id.newhao:// 版本号
			showProgressDialog("正在检查新版本,请稍等...");
			updateAgent();
			break;

		case R.id.mylocation:// 我的定位
			if (Common.isNetworkConnected(SettingActivity.this)) {
				intent = new Intent(SettingActivity.this, MyLoaction.class);
				startActivity(intent);
			} else {
				Common.DisplayToast(SettingActivity.this, "通信失败,请检查网络!", 1);
			}
			break;

		case R.id.finishs:// 退出
			new Builder(SettingActivity.this)
					.setMessage("您确认要退出该账号吗?")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog2,
										int which) {
									// 退出,注销后将缓存中的个人信息清理
									sp = getSharedPreferences(Const.PREF_NAME,
											Context.MODE_PRIVATE);
									sp.edit().clear().commit();

									// 退出用户,将用户的uid进行别名绑定取消(退出的是当前登录的用户)
//									setAlias("");
									JPushInterface.setAliasAndTags(
											getApplicationContext(), "", null,
											mAliasCallback);
									// 退出账号将推送的通知清除
									JPushInterface
											.clearAllNotifications(getApplicationContext());
									// 清除自定义消息
									NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
									mNotificationManager.cancelAll();
									
									//停止激光服务
									JPushInterface.stopPush(getApplicationContext());
									
									MyApplication.getInstace().exit();

								}
							}).setNegativeButton("取消", null).show();
			break;

		default:
			break;
		}
	}

	void updateAgent() {
		// UmengUpdateAgent.update(this);
		// UmengUpdateAgent.setUpdateAutoPopup(false);
		UmengUpdateAgent.forceUpdate(SettingActivity.this);
		// UmengUpdateAgent.setUpdateAutoPopup(false);
		UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
			@Override
			public void onUpdateReturned(int updateStatus,
					UpdateResponse updateInfo) {
				switch (updateStatus) {
				case 0: // has update
					closeProgressDialog();
					UmengUpdateAgent.showUpdateDialog(SettingActivity.this,
							updateInfo);
					break;
				case 1: // has no update
					closeProgressDialog();
					Toast.makeText(SettingActivity.this,
							"当前是最新版本,版本号为:" + version, Toast.LENGTH_SHORT)
							.show();
					set_location.setText("V" + version);
					break;
				case 2: // none wifi
					closeProgressDialog();
					Toast.makeText(SettingActivity.this, "没有wifi连接， 只在wifi下更新",
							Toast.LENGTH_SHORT).show();
					break;
				case 3: // time out
					closeProgressDialog();
					Toast.makeText(SettingActivity.this, "检查超时,请稍后再试...",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		});
	}

}
